<?PHP
/*
 * $Id: ArrayListTest.class.php 156 2009-12-16 15:59:00Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\utils;

use \Exception;
use \UnitTestCase;

use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\IllegalAccessException;

/**
 * @see UnitTestCase
 */
class ArrayListTest extends UnitTestCase
{
	/**
	 * @test
	 */
	public function test__construct()
	{
		$list = new ArrayList();
		$this->assertIdentical($list->size(), 0);
		$list = new ArrayList(1, 2, 3);
		$this->assertIdentical($list->size(), 3);
		$list = new ArrayList(array('a', 'b', 'c'));
		$this->assertIdentical($list->size(), 3);
		$list = new ArrayList(array('a' => 0, 'b' => 1, 'c' => 2));
		$this->assertIdentical($list->get(0), 0);
		$this->assertIdentical($list->get(1), 1);
		$this->assertIdentical($list->get(2), 2);
	}

	/**
	 * @test
	 */
	public function testSize()
	{
		$list = new ArrayList();
		$list->add('one');

		// #size
		$this->assertIdentical($list->size(), 1);
		$this->assertIdentical(count($list), 1);
		$this->assertIdentical($list->count(), 1);
	}

	/**
	 * @test
	 */
	public function testFirstElement()
	{
		$list = new ArrayList();
		$this->assertIdentical($list->firstElement(), null);
		$list->add('one');
		$this->assertIdentical($list->firstElement(),  'one');
	}

	/**
	 * @test
	 */
	public function testHasElement()
	{
		$list = new ArrayList();
		$this->assertIdentical($list->hasElement(), false);
		$list->add('one');
		$this->assertIdentical($list->hasElement(), true);
	}

	/**
	 * @test
	 */
	public function testGet()
	{
		$list = new ArrayList();
		$list->add('one');

		// #get
		$this->assertIdentical($list->get(0), 'one');
		try {
			echo $list->get(1);
			$this->fail();
		} catch (IndexOutOfBoundsException $e) {}
	}

	/**
	 * @test
	 */
	public function testSet()
	{
		$list = new ArrayList();
		$list->add('one');
		$list->add('two');
		$list->set(0, 1);
		$this->assertIdentical($list->get(0), 1);
		try {
			$list->set(2, '2');
			$this->fail();
		} catch (IndexOutOfBoundsException $e) {}
//		$list->set(2, '2', true);
//		$this->assertTrue($list->get(2) === '2');
		$this->assertIsA($list->add('tree'), 'barge\utils\ArrayList');
	}

	/**
	 * @test
	 */
	public function testArrayIndex()
	{
		$list = new ArrayList();
		$list->add('one');
		$list->add('two');
		$this->assertIdentical($list[0], 'one');
		$this->assertIdentical($list[1], 'two');
		try {
			echo $list[2];
			$this->fail();
		} catch (IndexOutOfBoundsException $e) {}
		// isset
		$this->assertTrue(isset($list[0]));
		$this->assertFalse(isset($list[2]));
		// unset
		try {
			unset($list[2]);
			$this->fail();
		} catch (IllegalAccessException $e) {}
	}

	/**
	 * @test
	 */
	public function testSubSequence()
	{
		$list = new ArrayList();
		$list->add(1);
		$list->add(2);
		$list->add(3);
		$list->add(4);
		$this->assertIdentical($list->size(), 4);
		$this->assertIdentical($list->subSequence(1)->size(), 3);
		$this->assertIdentical($list->subSequence(1, -1)->size(), 2);
		$this->assertIdentical($list->subSequence(1, 2)->size(), 2);
		$this->assertIdentical($list->subSequence(1, 100)->size(), 3);
		$this->assertIdentical($list->subSequence(-1)->size(), 1);
		$this->assertIdentical($list->subSequence(-1)->get(0), 4);
		$newlist = $list->subSequence(1, 2);
		$this->assertIdentical($newlist->get(0), 2);
		$this->assertIdentical($newlist->get(1), 3);
	}

	/**
	 * @test
	 */
	public function testAddAll()
	{
		$list = new ArrayList();
		$list->add(1);
		$list->add(2);
		$list->add(3);
		$list->add(4);
		$list->addAll(new ArrayList(5, 6));
		$this->assertIdentical($list->size(), 6);
		$list->addAll(array(7, 8));
		$this->assertIdentical($list->size(), 8);
		$this->assertTrue($list->addAll(array(9)) === $list);
		try {
			$list->addAll(1);
			$this->fail();
		} catch (Exception $e) {}
	}

	/**
	 * @test
	 */
	public function testRemove()
	{
		$list = new ArrayList(1, 2, 3, 4);
		$this->assertIdentical($list->size(), 4);
		$value = $list->remove(2);
		$this->assertIdentical($value, 3);
		$this->assertIdentical($list->size(), 3);
		$this->assertIdentical($list->get(0), 1);
		$this->assertIdentical($list->get(1), 2);
		$this->assertIdentical($list->get(2), 4);

		try {
			$list->remove(3);
			$this->fail();
		} catch (Exception $e) {}

		$list = new ArrayList(3, 1, 1, 4, 5);
		$value = $list->remove(2);
		$this->assertIdentical($value, 1);
		$this->assertIdentical($list->get(0), 3);
		$this->assertIdentical($list->get(1), 1);
		$this->assertIdentical($list->get(2), 4);
		$this->assertIdentical($list->get(3), 5);
	}

	/**
	 * @test
	 */
	public function testSort()
	{
		$list = new ArrayList(3, 1, 2, 4, 5);
		$sortedList = $list->sort(new ListComparator());
		$this->assertIdentical($sortedList->size(), 5);
		$this->assertIdentical($sortedList->get(0), 1);
		$this->assertIdentical($sortedList->get(1), 2);
		$this->assertIdentical($sortedList->get(2), 3);

		// 元配列への影響
		$this->assertIdentical($list->size(), 5);
		$this->assertIdentical($list->get(0), 3);
		$this->assertIdentical($list->get(1), 1);
		$this->assertIdentical($list->get(2), 2);

		// クロージャ使用
		$list = new ArrayList(3, 1, 2, 4, 5);
		$sortedList = $list->sort(function($a, $b) { return strcmp($a, $b); }  );
		$this->assertIdentical($sortedList->size(), 5);
		$this->assertIdentical($sortedList->get(0), 1);
		$this->assertIdentical($sortedList->get(1), 2);
		$this->assertIdentical($sortedList->get(2), 3);

	}

	/**
	 * @test
	 */
	public function testUnique()
	{
		$list = new ArrayList(3, 1, 1, 4, 5);
		$uniqueList = $list->unique();
		$this->assertIdentical($uniqueList->size(), 4);
		$this->assertIdentical($uniqueList->get(0), 3);
		$this->assertIdentical($uniqueList->get(1), 1);
		$this->assertIdentical($uniqueList->get(2), 4);
		$this->assertIdentical($uniqueList->get(3), 5);
	}

	/**
	 * @test
	 */
	public function testIndexOf()
	{
		$list = new ArrayList(3, 1, 1, 4, 5);
		$this->assertIdentical($list->indexOf(3), 0);
		$this->assertIdentical($list->indexOf(1), 1);
		$this->assertIdentical($list->indexOf(4), 3);
		$this->assertIdentical($list->indexOf(6), -1);
	}
}


class ListComparator implements Comparator
{
	public function compare($a, $b)
	{
		return strcmp($a, $b);
	}
}
